package ee.siimkaspar.subjects.service;

import java.util.List;

import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import ee.siimkaspar.subjects.model.Address;
import ee.siimkaspar.subjects.model.Contact;
import ee.siimkaspar.subjects.model.Enterprise;
import ee.siimkaspar.subjects.model.SubjectAttribute;
import ee.siimkaspar.subjects.model.SubjectAttributeType;
import ee.siimkaspar.subjects.model.SubjectType;

@Transactional
public class SubjectService extends BaseService {

	public boolean isSubjectCustomer(Integer subjectTypeId, Integer subjectId) {
		try {
			String sql = "select c from Customer c where c.subjectType.id=:subjectTypeId and c.subjectFk=:subjectId";
			Query q = getEntityManager().createQuery(sql);
			q.setParameter("subjectTypeId", subjectTypeId).setParameter("subjectId", subjectId);
			List<?> result = q.getResultList();
			return result.size() > 0;
		} catch(Exception e) {
			Logger.getLogger(getClass()).error("Error loading all subject types. Error msg=" + e.getMessage());
		}
		return false;
	}
	@SuppressWarnings("unchecked")
	public List<SubjectType> getAllSubjectTypes() {
		try {
			Query q = getEntityManager().createQuery("select st from SubjectType st");
			return q.getResultList();
		} catch(Exception e) {
			Logger.getLogger(getClass()).error("Error loading all subject types. Error msg=" + e.getMessage());
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<SubjectAttributeType> getAllSubjectAttributeTypes(Integer subjectTypeId) {
		try {
			Query q = getEntityManager().createQuery("select sat from SubjectAttributeType sat where sat.subjectType.id=:subjectTypeId order by sat.orderby");
			q.setParameter("subjectTypeId", subjectTypeId);
			return q.getResultList();
		} catch(Exception e) {
			Logger.getLogger(getClass()).error("Error loading all subject attribute types. Error msg=" + e.getMessage());
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<SubjectAttribute> getAllSubjectAttributes(Integer subjectTypeId, Integer subjectId) {
		try {
			String sql = "select sa from SubjectAttribute sa where sa.subjectType.id=:subjectTypeId and sa.subjectFk=:subjectId order by sa.orderby";
			Query q = getEntityManager().createQuery(sql);
			q.setParameter("subjectTypeId", subjectTypeId).setParameter("subjectId", subjectId);
			return q.getResultList();
		} catch(Exception e) {
			Logger.getLogger(getClass()).error("Error loading all subject attributes. Error msg=" + e.getMessage());
		}
		return null;
	}
	
	public Address getSubjectMainAddress(Integer subjectId, Integer addressType) {
		try {
			String sql = "select a from Address a where a.subjectFk=:subjectId and a.addressType.id=:addressType";
			Query q = getEntityManager().createQuery(sql);
			q.setParameter("subjectId", subjectId).setParameter("addressType", addressType);
			return (Address) q.getSingleResult();
		} catch(Exception e) {
			Logger.getLogger(getClass()).error("Error loading subject main address. Error msg=" + e.getMessage());
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Enterprise> getAllEnterprises() {
		try {
			Query q = getEntityManager().createQuery("select e from Enterprise e");
			return q.getResultList();
		} catch(Exception e) {
			Logger.getLogger(getClass()).error("Error loading all enterprises. Error msg=" + e.getMessage());
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Contact> getSubjectContacts(Integer subjectTypeId, Integer subjectId) {
		try {
			Query q = getEntityManager().createQuery("select c from Contact c where c.subjectType.id=:subjectTypeId and c.subjectFk=:subjectId order by orderby");
			q.setParameter("subjectTypeId", subjectTypeId).setParameter("subjectId", subjectId);
			return q.getResultList();
		} catch(Exception e) {
			Logger.getLogger(getClass()).error("Error loading all subject contacts. Error msg=" + e.getMessage());
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Contact> getAllSubjectAddresses(Integer subjectTypeId, Integer subjectId) {
		try {
			Query q = getEntityManager().createQuery("select a from Address a where a.subjectType.id=:subjectTypeId and a.subjectFk=:subjectId");
			q.setParameter("subjectTypeId", subjectTypeId).setParameter("subjectId", subjectId);
			return q.getResultList();
		} catch(Exception e) {
			Logger.getLogger(getClass()).error("Error loading all subject contacts. Error msg=" + e.getMessage());
		}
		return null;
	}
	
}
